package aishua.com.mytestapplist.bean;

import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import aishua.com.mytestapplist.coredata.ApduInfo;
import aishua.com.mytestapplist.utils.CommonMethods;


public class ReqAppletApduChatBean extends BaseBeanReq {
	
	/** Ӧ��AID */
	private String appAID;
	
	/** Ӧ�ð汾�� */
	private String appletVersion;
	
	/** ����ϵͳ���� */
	private String osType;
	
	
	/** Ӧ�ò������ͣ�
		01����
		02����
		03����
		04ɾ��
	 */
	private String operType;
	
	/**
	 * �������к�
	 */
	private short taskIndex;
	
	/**
	 * 0���ɹ� 1��ʧ��
	 */
	private String result;
	
	/** Ӧ���б����� */
	private List<ApduInfo> resApduInfoList;

	public String getAppAID() {
		return appAID;
	}

	public void setAppAID(String appAID) {
		this.appAID = appAID;
	}

	public String getAppletVersion() {
		return appletVersion;
	}

	public void setAppletVersion(String appletVersion) {
		this.appletVersion = appletVersion;
	}

	public String getOsType() {
		return osType;
	}

	public void setOsType(String osType) {
		this.osType = osType;
	}


	public String getOperType() {
		return operType;
	}

	public void setOperType(String operType) {
		this.operType = operType;
	}

	
	public List<ApduInfo> getResApduInfoList() {
		return resApduInfoList;
	}

	public void setResApduInfoList(List<ApduInfo> resApduInfoList) {
		this.resApduInfoList = resApduInfoList;
	}

	public short getTaskIndex() {
		return taskIndex;
	}

	public void setTaskIndex(short taskIndex) {
		this.taskIndex = taskIndex;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	private String version;
	private String businessType;
	private String traceNo;
	private String imei;
	private String imsi;
	private String phone;
	private String seid;
	
	public String getSeid() {
		return seid;
	}

	public void setSeid(String seid) {
		this.seid = seid;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getBusinessType() {
		return businessType;
	}

	public void setBusinessType(String businessType) {
		this.businessType = businessType;
	}

	public String getTraceNo() {
		return traceNo;
	}

	public void setTraceNo(String traceNo) {
		this.traceNo = traceNo;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getImsi() {
		return imsi;
	}

	public void setImsi(String imsi) {
		this.imsi = imsi;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String buildPackage() {
		calMac();
		XmlSerializer xSerializer = Xml.newSerializer();
		StringWriter xmlWriter = new StringWriter();
		try {
			xSerializer.setOutput(xmlWriter);
			xSerializer.startDocument("UTF-8", true);
			
			xSerializer.startTag("", "tsm");
			xSerializer.attribute("", "version", getVersion());
			
			xSerializer.startTag("", "keyVersion");
			xSerializer.text(getKeyVersion());
			xSerializer.endTag("", "keyVersion");
			
			xSerializer.startTag("", "keyIndex");
			xSerializer.text(getKeyIndex());
			xSerializer.endTag("", "keyIndex");
			
			xSerializer.startTag("", "clientType");
			xSerializer.text(getClientType());
			xSerializer.endTag("", "clientType");
			
			xSerializer.startTag("", "clientVersion");
			xSerializer.text(getClientVersion());
			xSerializer.endTag("", "clientVersion");
			
				xSerializer.startTag("", "business");
				xSerializer.attribute("", "type", getBusinessType());
				
						xSerializer.startTag("", "traceNo");
						xSerializer.text(getTraceNo());
						xSerializer.endTag("", "traceNo");
				
					xSerializer.startTag("", "carrier");
					
					xSerializer.startTag("", "imsi");
					xSerializer.text(getImsi());
					xSerializer.endTag("", "imsi");
					
					xSerializer.startTag("", "seid");
					xSerializer.text(getSeid());
					xSerializer.endTag("", "seid");
					
					xSerializer.endTag("", "carrier");
				
						xSerializer.startTag("", "te");
						xSerializer.startTag("", "imei");
						xSerializer.text(getImei());
						xSerializer.endTag("", "imei");
						xSerializer.startTag("", "phone");
						xSerializer.text(getPhone());
						xSerializer.endTag("", "phone");
						xSerializer.endTag("", "te");
				
				if (getResApduInfoList() == null) {
						// �״�ͨ��
						xSerializer.startTag("", "appAID");
						xSerializer.text(getAppAID());
						xSerializer.endTag("", "appAID");
						
						xSerializer.startTag("", "appletVersion");
						xSerializer.text(getAppletVersion());
						xSerializer.endTag("", "appletVersion");
						
						
						xSerializer.startTag("", "osType");
						xSerializer.text(getOsType());
						xSerializer.endTag("", "osType");
						
						xSerializer.startTag("", "operType");
						xSerializer.text(getOperType());
						xSerializer.endTag("", "operType");
				} else {
					// ����APDU
					xSerializer.startTag("", "RAPDUList");
					
					for (ApduInfo apduInfo:getResApduInfoList()) {
						xSerializer.startTag("", "GAPDUInfomation");
						
						xSerializer.startTag("", "index");
						xSerializer.text(String.valueOf(apduInfo.getIndex()));
						xSerializer.endTag("", "index");
						
						xSerializer.startTag("", "APDU");
						xSerializer.text(apduInfo.getApdu());
						xSerializer.endTag("", "APDU");
						
						
						for (int i=0; i<apduInfo.getSwList().size(); i++) {
							xSerializer.startTag("", "SW");
							xSerializer.text(apduInfo.getSwList().get(i));
							xSerializer.endTag("", "SW");
						}
						
						
						xSerializer.endTag("", "GAPDUInfomation");
					}
					xSerializer.endTag("", "RAPDUList");
					
					xSerializer.startTag("", "Result");
					xSerializer.text(getResult());
					xSerializer.endTag("", "Result");
				}
				
				xSerializer.startTag("", "TaskIndex");
				
				xSerializer.text(CommonMethods.bytesToHexString(
						CommonMethods.short2OctBytes(getTaskIndex())));
				xSerializer.endTag("", "TaskIndex");
				
				xSerializer.endTag("", "business");
				
			xSerializer.startTag("", "mac");
			xSerializer.text(getMac());
			xSerializer.endTag("", "mac");
			
			xSerializer.endTag("", "tsm");
			
			xSerializer.endDocument();
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xmlWriter.toString();
	}
	
	private void calMac() {
		setMac("12345678");
	}
	
}
